<template>
    <div style="padding:20px;">
        <el-table :data="users">
            <el-table-column prop="name" label="用户" width="120">
            </el-table-column>
            <el-table-column prop="phone" label="手机" width="120">
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="200">
            </el-table-column>
            <el-table-column prop="lastLoginTime" label="最后登录时间" width="200">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    @click="editUser(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                    size="mini"
                    type="danger"
                    @click="changePwd(scope.$index, scope.row)">修改密码</el-button>
                    <el-button
                    size="mini"
                    type="danger"
                    @click="removeUser(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog
            title="编辑用户"
            :visible.sync="editVisible"
            width="30%"
            center>
            <user-edit @submit="submitEdit" @close="editVisible = false" :user="user"></user-edit>
        </el-dialog>

        <el-dialog
            title="重置密码"
            :visible.sync="resetVisible"
            width="30%"
            center>
            <change-password @submit="submitEdit" @close="resetVisible = false" :user="user"></change-password>
        </el-dialog>
    </div>
</template>

<script>
import changePassword from './components/changePassword';
import userEdit from './components/userEdit';
import { parseTime } from '../../utils';
export default {
    name: 'allAnchor',
    components: { userEdit, changePassword },
    data() {
        return {
            users: [],
            pageInfo: {},
            editVisible: false,
            resetVisible: false,
            user: {} //待编辑的用户
        };
    },
    methods: {
        queryAll(tag) {
            let params = {
                pageNum: 1,
                pageSize: 20
            };
            if (tag) {
                params.status = tag;
            }
            this.$ajax
                .get('/user/queryAll', {
                    params
                })
                .then(res => {
                    const { list, pageNum, pageSize, pages, total } = res.data;
                    this.users = list.map(item => {
                        item.lastLoginTime = parseTime(item.lastLoginTime);
                        item.createTime = parseTime(item.createTime);
                        return item;
                    });
                    this.pageInfo = {
                        pageNum,
                        pageSize,
                        pages,
                        total
                    };
                });
        },
        changePwd(index, rows) {
            this.resetVisible = true;
            this.user = rows;
        },
        editUser(index, rows) {
            this.editVisible = true;
            this.user = rows;
        },
        removeUser(index, rows) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$ajax
                        .post('/user/admin/remove', {
                            id: this.user.id
                        })
                        .then(res => {
                            if (res.code === 1) {
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                this.$nextTick(() => {
                                    this.queryAll();
                                });
                            } else {
                                this.$message.error('用户删除失败!');
                            }
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        submitEdit() {
            this.editVisible = false;
            this.resetVisible = false;
            this.$nextTick(() => {
                this.queryAll();
            });
        }
    },
    mounted() {
        this.queryAll();
    }
};
</script>

